<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_conn.php';

function is_end_acc($parentId) {
    $pdb = new acc_DB();
    $ssql = "select count(*) as __cc from account where parentId = " . $parentId;
    $c = $pdb->qw($ssql, "__cc"); //假设 $c 返回的是 68,则 $c==68 为true   而$c=='68'也为true
    $pdb->d();
    return (0 == $c) ? 1 : 0;
}

function getaccfullpath($sn, &$subs) {
    $pdb = new acc_DB();
    $ssql = "select * from account where serialNo = '" . $sn . "'";
    $rst = $pdb->q($ssql);
    $pr = $rst->fetchArray(SQLITE3_ASSOC);
    
    if($pr) {
        $subs = $pr["name"] . "-" . $subs;    
        if ("0" == $pr["parentId"]) {
            //nothing to do
        } else {
            getaccfullpath($pr["parentId"], $subs);
        }
    }
    $pdb->d();
}

function mysingle(&$r) {
    if(0 == is_end_acc($r["id"])) { //不是叶子会计科目 就返回,直到找到叶子终端会计科目才进行处理
        return;
    }

    $s = "";
    getaccfullpath($r["serialNo"], $s);
    $s = preg_replace("/(.*)-$/", "$1", $s);

    $ha = "";
    if(preg_match('/(\d)(\d*)/', $r["serialNo"], $mts)) { //取出会计科目的首位
        $ha = $mts[1];
    }
    switch ($ha) {
        case "1":
            $ha = "资产";
            break;
        case "2":
            $ha = "负债";
            break;
        case "3":
            $ha = "权益";
            break;
        case "4":
            $ha = "成本";
            break;
        case "5":
            $ha = "损益";
            break;
        default:
            $ha = "未分类";
            break;
    }

    return '{label: "' . $r["serialNo"] . ", " . $s . '", category: "' . $ha . '", v:' . $r["id"] . '},' . "\n";
}

function get_acc_source() {
    $pdb = new acc_DB();
    $rst = $pdb->q("select * from account");
    $str = "";
    
    while($r = $rst->fetchArray(SQLITE3_ASSOC)) {
        $str .= mysingle($r);
    }
    $pdb->d();
    return $str;
}
//----------------------------------------------------------------------------useless backup.
function build1arr($parentId, &$str, $rstr) {
    $pdb = new acc_DB();
    $ssql = "select * from account where parentId = " . $parentId;
    $rst = $pdb->q($ssql);

    while($row = $rst->fetchArray(SQLITE3_ASSOC)) {
        $end_acc = is_end_acc($row["id"]);
                
        if (1 == $end_acc) {
            $str .= $row["serialNo"] . ", " . $rstr . $row["name"] . "<br/>";
        } else {
            $rstr .= $row["name"] . "-";
            build1arr($row["serialNo"], $str, $rstr);
        }
    }
    $pdb->d();
}

function aaa () {
    $str = "";
    $rstr = "";
    build1arr(0, $str, $rstr);
    echo $str;
}

function build1item($parentId, &$str) {
    $pdb = new acc_DB();
    $ssql = "select count(*) as __cc from account where parentId = " . $parentId;
    if (0 == $pdb->qw($ssql, "__cc")) {
        $pdb->d();
        return;
    }
    $rst = $pdb->q($ssql);
    
    while($row = $rst->fetchArray(SQLITE3_ASSOC)) {
        $lv = $row["level"];
        for($i = 0; $i < $lv; ++$i) {
            $str .= "    ";
        }
        $str .= '<accItem id="' . $row["serialNo"] . '" name="' . $row["name"] . '">' . "\n";
        build1item($row["serialNo"], $str);
        
        for($i = 0; $i < $lv; ++$i) {
            $str .= "    ";
        }
        $str .= "</accItem>\n";
    }
    $pdb->d();
}

function rebuildxml() {
    $str = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
    $str .= '<accc>' . "\n";
    build1item(0, $str);
    $str .= '</accc>';
    
    $file = fopen("out_acc.xml","w");
    if (!is_resource($file)) {
        exit ("can not open file for write.");
    }
     
    if(!fwrite($file, $str)) {
        exit ("write outacc.xml error");
    }
    
    if(!fclose($file)){
        exit ("close out_acc.xml Error.");
    }

    echo "ok";
}
//rebuildxml();


function build_accxml_______backup($tarfn) {
    $pdb = new acc_DB();

    $istr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<acc_for_client>
</acc_for_client>
XML;
    $mx = new SimpleXMLElement($istr);
    $ssql = "select * from account where parentId = 0";
    $rst = $pdb->q($ssql);
    
    while($row = $rst->fetchArray(SQLITE3_ASSOC)) {
        $mx->addChild("ai", $row["name"]);
        $mx->ai[sizeof($mx->ai) - 1]->addAttribute("i", $row["serialNo"]);
    }

    echo $mx->asXML();
    $pdb->d();    
}

?>
